package com.vikstrom.LTimePictureViewer;

import android.os.Environment;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author kaolick
 */
public class StorageHelper {
    // Storage states
    private boolean externalStorageAvailable, externalStorageWriteable, bilderFolderAvailable;
    private File bilderFolder;

    public StorageHelper() {
        bilderFolderAvailable = false;
    }
    /**
     * Checks the external storage's state and saves it in member attributes.
     */
    private void checkStorage() {
        // Get the external storage's state
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            // Storage is available and writeable
            externalStorageAvailable = externalStorageWriteable = true;
        } else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
            // Storage is only readable
            externalStorageAvailable = true;
            externalStorageWriteable = false;
        } else {
            // Storage is neither readable nor writeable
            externalStorageAvailable = externalStorageWriteable = false;
        }
    }

    /**
     * Checks the state of the external storage.
     *
     * @return True if the external storage is available, false otherwise.
     */
    public boolean isExternalStorageAvailable(String folderName) {
        checkStorage();
        if (externalStorageAvailable) {
            // check for folder
            if (!folderName.equals("")) {
                File[] extStorage = Environment.getExternalStorageDirectory().listFiles();

                boolean bilderFolderFound = false;
                for (File f : extStorage) {
                    if (f.isDirectory() &&
                            f.getName().equals(folderName)) {
                        bilderFolder = f;
                        bilderFolderFound = true;
                        //Log.d("bilderFolder", f.getAbsolutePath());
                        break;
                    }
                }
                if (bilderFolderFound) {
                    bilderFolderAvailable = true;
                } else
                    bilderFolderAvailable = false;

                return bilderFolderAvailable;
            } else
                return true;
        } else
            return false;
    }

    /**
     * Checks the state of the external storage.
     *
     * @return True if the external storage is writeable, false otherwise.
     */
    public boolean isExternalStorageWriteable() {
        checkStorage();
        return externalStorageWriteable;
    }

    /**
     * Checks the state of the external storage.
     *
     * @return True if the external storage is available and writeable, false
     *         otherwise.
     */
    public boolean isExternalStorageAvailableAndWriteable() {
        checkStorage();
        if (!externalStorageAvailable) {
            return false;
        } else if (!externalStorageWriteable) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Get all the pictures in bilderFolder
     *
     * @return ArrayList with picture files
     */
    public ArrayList<LPicture> getPictures(String folderName) {
        ArrayList<LPicture> pictures = new ArrayList<LPicture>();
        isExternalStorageAvailable(folderName);
        if (bilderFolderAvailable) {

            ArrayList<LPicture> allFiles = new ArrayList<LPicture>();
            Queue<File> dirs = new LinkedList<File>();
            dirs.add(bilderFolder);
            while (!dirs.isEmpty()) {
                for (File f : dirs.poll().listFiles()) {
                    if (f.isDirectory()) {
                        dirs.add(f);
                    } else if (f.isFile()) {
                        if (isImage(f)) {
                            String name = getImageName(f);
                            String description = getImageFolder(f);
                            LPicture p = new LPicture(f, name, description);
                            pictures.add(p);
                        }
                    }
                }
            }
        }
        return pictures;
    }

    private boolean isImage(File file) {
        final String[] okFileExtensions =  new String[] {"jpg", "png", "gif","jpeg"};

        for (String extension : okFileExtensions)
        {
            if (file.getName().toLowerCase().endsWith(extension))
            {
                //Log.d("isImage", "ok - "+file.getName());
                return true;
            } else {
                //Log.d("isImage", "fail - "+file.getName());
            }
        }
        return false;
    }

    private String getImageName(File file) {
        return file.getName();
    }

    private String getImageFolder(File file) {
        File parent = file.getParentFile();
        int nameCount = bilderFolder.getAbsolutePath().length();
        String name = parent.getAbsolutePath();
        if (name.length() > nameCount) {
            name = name.substring(nameCount+1);
        } else {
            name = "";
        }
        //Log.d("getImageName", name);
        return name;
    }
}
